Complete Java Star Patterns Guide
๐ Table of Contentsโ
CATEGORY A: Basic Patterns โญ Most Askedโ
- Square Pattern
- Hollow Square Pattern
- Right Triangle Pattern
- Left Triangle Pattern
- Inverted Right Triangle
- Inverted Left Triangle
CATEGORY B: Pyramid Patterns ๐ฅ Very Commonโ
- Pyramid Pattern
- Inverted Pyramid Pattern
- Hollow Pyramid Pattern
- Diamond Pattern
- Hollow Diamond Pattern
- Half Diamond Pattern
CATEGORY C: Advanced Patterns ๐ Interview Favoritesโ
- Butterfly Pattern
- Hourglass Pattern
- Pascal's Triangle
- Floyd's Triangle
- Number Pyramid
- Palindrome Pattern
CATEGORY D: Specialty Patterns โจโ
CATEGORY A: Basic Patterns โญโ
1. Square Patternโ
Difficulty: Easy | Asked by: TCS, Wipro, Infosys
Output:
*****
*****
*****
*****
*****
Java Code:
public static void squarePattern(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print("* ");
}
System.out.println();
}
}
Logic: Simple nested loop - outer for rows, inner for columns.
2. Hollow Square Patternโ
Difficulty: Easy | Asked by: Accenture, Cognizant
Output:
*****
* *
* *
* *
*****
Java Code:
public static void hollowSquarePattern(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Logic: Print stars only on borders (first/last row or first/last column).
3. Right Triangle Patternโ
Difficulty: Easy | Asked by: Amazon, Microsoft, Google
Output:
*
**
***
****
*****
Java Code:
public static void rightTrianglePattern(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
Logic: Print stars equal to current row number.
4. Left Triangle Patternโ
Difficulty: Easy | Asked by: Adobe, Oracle
Output:
*
**
***
****
*****
Java Code:
public static void leftTrianglePattern(int n) {
for (int i = 1; i <= n; i++) {
// Print spaces
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// Print stars
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
Logic: Print (n - i) spaces, then i stars for each row i.
5. Inverted Right Triangleโ
Difficulty: Easy | Asked by: Capgemini, IBM
Output:
*****
****
***
**
*
Java Code:
public static void invertedRightTriangle(int n) {
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
Logic: Start from n and decrement, print stars equal to current value.
6. Inverted Left Triangleโ
Difficulty: Easy | Asked by: HCL, Tech Mahindra
Output:
*****
****
***
**
*
Java Code:
public static void invertedLeftTriangle(int n) {
for (int i = 0; i < n; i++) {
// Print spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// Print stars
for (int j = 0; j < n - i; j++) {
System.out.print("*");
}
System.out.println();
}
}
Logic: Print i spaces, then (n - i) stars for each row i.
CATEGORY B: Pyramid Patterns ๐ฅโ
7. Pyramid Patternโ
Difficulty: Medium | Asked by: Amazon, Flipkart, Paytm
Output:
*
***
*****
*******
*********
Java Code:
public static void pyramidPattern(int n) {
for (int i = 0; i < n; i++) {
// Print spaces
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
// Print stars
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
Logic: Print (n - i - 1) spaces, then (2 * i + 1) stars.
8. Inverted Pyramid Patternโ
Difficulty: Medium | Asked by: Google, Facebook
Output:
*********
*******
*****
***
*
Java Code:
public static void invertedPyramidPattern(int n) {
for (int i = 0; i < n; i++) {
// Print spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// Print stars
for (int j = 0; j < 2 * (n - i) - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
Logic: Print i spaces, then (2 * (n - i) - 1) stars.
9. Hollow Pyramid Patternโ
Difficulty: Medium | Asked by: Microsoft, Adobe
Output:
*
* *
* *
* *
*********
Java Code:
public static void hollowPyramidPattern(int n) {
for (int i = 0; i < n; i++) {
// Print spaces
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
// Print stars
for (int j = 0; j < 2 * i + 1; j++) {
if (j == 0 || j == 2 * i || i == n - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Logic: Print stars only at boundaries and last row.
10. Diamond Patternโ
Difficulty: Medium | Asked by: Amazon, Microsoft, Google
Output:
*
***
*****
*******
*********
*******
*****
***
*
Java Code:
public static void diamondPattern(int n) {
// Upper pyramid
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
// Lower inverted pyramid
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
Logic: Combine pyramid and inverted pyramid.
11. Hollow Diamond Patternโ
Difficulty: Hard | Asked by: Oracle, SAP
Output:
*
* *
* *
* *
* *
* *
* *
* *
*
Java Code:
public static void hollowDiamondPattern(int n) {
// Upper half
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i + 1; j++) {
if (j == 0 || j == 2 * i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// Lower half
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i + 1; j++) {
if (j == 0 || j == 2 * i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Logic: Hollow pyramid + hollow inverted pyramid.
12. Half Diamond Patternโ
Difficulty: Medium | Asked by: Infosys, TCS
Output:
*
**
***
****
*****
****
***
**
*
Java Code:
public static void halfDiamondPattern(int n) {
// Upper half
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// Lower half
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
Logic: Right triangle + inverted right triangle.
CATEGORY C: Advanced Patterns ๐โ
13. Butterfly Patternโ
Difficulty: Hard | Asked by: Amazon, Google, Microsoft
Output:
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
Java Code:
public static void butterflyPattern(int n) {
// Upper half
for (int i = 1; i <= n; i++) {
// Left stars
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Spaces
for (int j = 1; j <= 2 * (n - i); j++) {
System.out.print(" ");
}
// Right stars
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// Lower half
for (int i = n - 1; i >= 1; i--) {
// Left stars
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Spaces
for (int j = 1; j <= 2 * (n - i); j++) {
System.out.print(" ");
}
// Right stars
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
Logic: Mirror two triangles with spaces in between.
14. Hourglass Patternโ
Difficulty: Hard | Asked by: Adobe, Oracle
Output:
*********
*******
*****
***
*
***
*****
*******
*********
Java Code:
public static void hourglassPattern(int n) {
// Upper inverted pyramid
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * (n - i) - 1; j++) {
System.out.print("*");
}
System.out.println();
}
// Lower pyramid (skip middle line)
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
Logic: Inverted pyramid + pyramid (skip center).
15. Pascal's Triangleโ
Difficulty: Hard | Asked by: Google, Facebook, Amazon
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Java Code:
public static void pascalsTriangle(int n) {
for (int i = 0; i < n; i++) {
// Print spaces
for (int j = 0; j < n - i - 1; j++) {
System.out.print(" ");
}
int num = 1;
for (int j = 0; j <= i; j++) {
System.out.print(num + " ");
num = num * (i - j) / (j + 1);
}
System.out.println();
}
}
Logic: Use binomial coefficient formula: C(n,k) = C(n,k-1) * (n-k+1) / k
16. Floyd's Triangleโ
Difficulty: Medium | Asked by: TCS, Infosys, Wipro
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Java Code:
public static void floydsTriangle(int n) {
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
Logic: Print consecutive numbers in triangular form.
17. Number Pyramidโ
Difficulty: Medium | Asked by: Amazon, Microsoft
Output:
1
121
12321
1234321
123454321
Java Code:
public static void numberPyramid(int n) {
for (int i = 1; i <= n; i++) {
// Print spaces
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// Print ascending numbers
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
// Print descending numbers
for (int j = i - 1; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
}
Logic: Print ascending then descending numbers.
18. Palindrome Patternโ
Difficulty: Medium | Asked by: Cognizant, Accenture
Output:
1
212
32123
4321234
543212345
Java Code:
public static void palindromePattern(int n) {
for (int i = 1; i <= n; i++) {
// Print spaces
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// Print descending numbers
for (int j = i; j >= 1; j--) {
System.out.print(j);
}
// Print ascending numbers (skip 1)
for (int j = 2; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
Logic: Print descending then ascending numbers.
CATEGORY D: Specialty Patterns โจโ
19. Heart Patternโ
Difficulty: Hard | Asked by: Rare, but popular
Output:
*** ***
***** *****
***********
*********
*******
*****
***
*
Java Code:
public static void heartPattern(int n) {
// Upper part
for (int i = n / 2; i < n; i += 2) {
// Left spaces
for (int j = 1; j < n - i; j += 2) {
System.out.print(" ");
}
// Left stars
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
// Middle spaces
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// Right stars
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// Lower inverted pyramid
for (int i = n; i > 0; i--) {
for (int j = 0; j < n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i * 2 - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
Logic: Two curves at top + inverted pyramid at bottom.
20. Plus Patternโ
Difficulty: Easy | Asked by: Accenture
Output:
*
*
*****
*
*
Java Code:
public static void plusPattern(int n) {
int mid = n / 2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == mid || j == mid) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Logic: Print stars at middle row or middle column.
21. Cross Patternโ
Difficulty: Easy | Asked by: Capgemini
Output:
* *
* *
*
* *
* *
Java Code:
public static void crossPattern(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j || i + j == n - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Logic: Print stars on main diagonal and anti-diagonal.
22. Zig-Zag Patternโ
Difficulty: Medium | Asked by: Oracle
Output:
* *
* * * *
* * *
Java Code:
public static void zigZagPattern(int n) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < n; j++) {
if (((i + j) % 4 == 0) || (i == 2 && j % 4 == 0)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Logic: Use modulo arithmetic for wave positions.
23. Sandglass Patternโ
Difficulty: Hard | Asked by: IBM
Output:
*********
* *
* *
* *
*
* *
* *
* *
*********
Java Code:
public static void sandglassPattern(int n) {
// Upper part
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * (n - i) - 1; j++) {
if (i == 0 || j == 0 || j == 2 * (n - i) - 2) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// Lower part
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int j = 0; j < 2 * (n - i) - 1; j++) {
if (i == 0 || j == 0 || j == 2 * (n - i) - 2) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Logic: Hollow inverted pyramid + hollow pyramid.
24. Wave Patternโ
Difficulty: Medium | Asked by: Tech Mahindra
Output:
* * *
* * * * * *
* * * *
Java Code:
public static void wavePattern(int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if ((i + j) % 4 == 0 || (i == 2 && j % 2 == 0) ||
(i == 1 && (j % 2 == 1))) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Logic: Create wave effect using modulo calculations.
๐ฏ Main Method - Test All Patternsโ
public class StarPatternsDemo {
public static void main(String[] args) {
int n = 5; // Default size
System.out.println("=== CATEGORY A: BASIC PATTERNS ===");
squarePattern(n);
hollowSquarePattern(n);
rightTrianglePattern(n);
leftTrianglePattern(n);
invertedRightTriangle(n);
invertedLeftTriangle(n);
System.out.println("\n=== CATEGORY B: PYRAMID PATTERNS ===");
pyramidPattern(n);
invertedPyramidPattern(n);
hollowPyramidPattern(n);
diamondPattern(n);
hollowDiamondPattern(n);
halfDiamondPattern(n);
System.out.println("\n=== CATEGORY C: ADVANCED PATTERNS ===");
butterflyPattern(n);
hourglassPattern(n);
pascalsTriangle(n);
floydsTriangle(n);
numberPyramid(n);
palindromePattern(n);
System.out.println("\n=== CATEGORY D: SPECIALTY PATTERNS ===");
heartPattern(6);
plusPattern(n);
crossPattern(n);
zigZagPattern(20);
sandglassPattern(n);
wavePattern(3, 20);
}
}
๐ Quick Tips for Interviewsโ
-
Understand the Logic First: Draw the pattern on paper and identify the logic for spaces and stars.
-
Common Approach: Most patterns use nested loops:
- Outer loop for rows
- Inner loop(s) for columns, spaces, and stars
-
Formula Patterns:
- Right triangle: stars = row number
- Pyramid: spaces = (n - i - 1), stars = (2 * i + 1)
- Diamond: Combine pyramid + inverted pyramid
-
Practice These Most:
- Right Triangle
- Pyramid
- Diamond
- Butterfly
- Number patterns
-
Time Complexity: Most patterns are O(nยฒ)
๐ Company-Wise Most Askedโ
- Amazon, Google, Microsoft: Pyramid, Diamond, Butterfly
- TCS, Infosys, Wipro: Floyd's Triangle, Pascal's Triangle, Basic patterns
- Accenture, Cognizant: Hollow patterns, Number patterns
- Adobe, Oracle: Complex combinations, Hourglass
Pro Tip: Master the basic patterns first, then combine them to create complex patterns!